package plugin

import (
	"fmt"
	"testing"
	"time"

	_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
	client "github.com/influxdata/influxdb1-client/v2"
)

func TestClient(t *testing.T) {
	c, err := client.NewHTTPClient(client.HTTPConfig{
		Addr:     "xxx",
		Username: "xxx",
		Password: "xxx",
	})
	if err != nil {
		fmt.Println("Error creating InfluxDB Client: ", err.Error())
	}
	pt, err := client.NewPoint(
		"cpu_usage",
		map[string]string{"bucket": "grafana"},
		map[string]interface{}{"number": 90},
		time.Now(),
	)
	if err != nil {
		fmt.Println("Error creating data point: ", err.Error())
		return
	}

	bp, err := client.NewBatchPoints(client.BatchPointsConfig{
		RetentionPolicy: "7d_events",
		Database:        "test",
		Precision:       "ns",
	})
	if err != nil {
		fmt.Println("Error creating batch points: ", err.Error())
		return
	}

	bp.AddPoint(pt)

	if err := c.Write(bp); err != nil {
		fmt.Println("Error writing to InfluxDB: ", err.Error())
	}

	/* q := client.NewQuery("INSERT INTO demo,tag1=1,tag2=3 field1=11,field2=22", "test", "ns")
	if response, err := c.Query(q); err == nil && response.Error() == nil {
		fmt.Println("the result is: ", response.Results)
	} */
}
